<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      db/Redis系列/08-Redis系列之-Redis-Cluster | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>db/Redis系列/08-Redis系列之-Redis-Cluster</h2>



  <p class="post-date">2019-12-29</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="一-Redis-Cluser介绍背景"><a href="#一-Redis-Cluser介绍背景" class="headerlink" title="一  Redis Cluser介绍背景"></a>一  Redis Cluser介绍背景</h2><h3 id="1-1问题"><a href="#1-1问题" class="headerlink" title="1.1问题"></a>1.1问题</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 存在问题 </span></span><br><span class="line"><span class="number">1</span> 并发量：单机redis qps为<span class="number">10</span>w/s,但是我们可能需要百万级别的并发量</span><br><span class="line"><span class="number">2</span> 数据量：机器内存<span class="number">16</span>g-<span class="number">-256</span>g，如果存<span class="number">500</span>g数据呢？</span><br></pre></td></tr></table></figure>

<h3 id="1-2-解决"><a href="#1-2-解决" class="headerlink" title="1.2 解决"></a>1.2 解决</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 解决：加机器，分布式</span></span><br><span class="line">redis cluster 在<span class="number">2015</span>年的 <span class="number">3.0</span> 版本加入了，满足分布式的需求</span><br></pre></td></tr></table></figure>

<h2 id="二-数据分布（分布式数据库）"><a href="#二-数据分布（分布式数据库）" class="headerlink" title="二 数据分布（分布式数据库）"></a>二 数据分布（分布式数据库）</h2><h3 id="2-1-存在问题"><a href="#2-1-存在问题" class="headerlink" title="2.1 存在问题"></a>2.1 存在问题</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">假设全量的数据非常大，<span class="number">500</span>g，单机已经无法满足，我们需要进行分区，分到若干个子集中</span><br></pre></td></tr></table></figure>

<h2 id="2-2-分区方式"><a href="#2-2-分区方式" class="headerlink" title="2.2 分区方式"></a>2.2 分区方式</h2><table>
<thead>
<tr>
<th>分布方式</th>
<th>特点</th>
<th>产品</th>
</tr>
</thead>
<tbody><tr>
<td>哈希分布</td>
<td>数据分散度高，建值分布于业务无关，无法顺序访问，支持批量操作</td>
<td>一致性哈希memcache，redis cluster，其他缓存产品</td>
</tr>
<tr>
<td>顺序分布</td>
<td>数据分散度易倾斜，建值业务相关，可顺序访问，支持批量操作</td>
<td>BigTable，HBase</td>
</tr>
</tbody></table>
<h4 id="2-2-1-顺序分区"><a href="#2-2-1-顺序分区" class="headerlink" title="2.2.1 顺序分区"></a>2.2.1 顺序分区</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 原理：100个数据分到3个节点上 1--33第一个节点；34--66第二个节点；67--100第三个节点（很多关系型数据库使用此种方式）</span></span><br></pre></td></tr></table></figure>

<h3 id="2-2-2-哈希分区"><a href="#2-2-2-哈希分区" class="headerlink" title="2.2.2 哈希分区"></a>2.2.2 哈希分区</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 原理：hash分区： 节点取余 ，假设3台机器， hash(key)%3,落到不同节点上</span></span><br></pre></td></tr></table></figure>

<h4 id="2-2-2-1-节点取余分区"><a href="#2-2-2-1-节点取余分区" class="headerlink" title="2.2.2 .1 节点取余分区"></a>2.2.2 .1 节点取余分区</h4><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfyki8z3j31z40s0e55.jpg" alt="image-20200811120153068"></p>
<p>节点扩容，添加一个节点，存在问题，很多数据需要偏移，总偏移量要大于80%</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfz99dh4j30x80u0dy9.jpg" alt="image-20200811120704296" style="zoom:30%;" />

<p>推荐翻倍扩容，由3变成6，数据量迁移为50%，比80%降低</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfzd2rulj31q80u04qp.jpg" alt="image-20200811120815523" style="zoom:30%;" />

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 总结：</span></span><br><span class="line">客户端分片，通过hash+取余</span><br><span class="line">节点伸缩，数据节点关系发生变化，导致影响数据迁移过大</span><br><span class="line">迁移数量和添加节点数量有关：建议翻倍扩容</span><br></pre></td></tr></table></figure>



<h4 id="2-2-2-2-一致性哈希分区"><a href="#2-2-2-2-一致性哈希分区" class="headerlink" title="2.2.2 .2 一致性哈希分区"></a>2.2.2 .2 一致性哈希分区</h4><p>每个节点负责一部分数据，对key进行hash，得到结果在node1和node2之间，就放到node2中，顺时针查找</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfzhs7wtj31i40u04e7.jpg" alt="image-20200811121233804" style="zoom:30%;" />

<p>假设添加一个新节点node5，现在只需要迁移一小部分数据，不会影响node3和node4的数据，只会迁移node1和node2的数据</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfzlkae8j30u00vagwo.jpg" alt="image-20200811121518855" style="zoom:30%;" />

<p>节点比较多的话合适，假设有1000个节点，加一个只要迁移千分之一的数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#总结：</span></span><br><span class="line">客户端分片：哈希+顺时针（优化取余）</span><br><span class="line">节点伸缩：只影响临近节点，但是还有数据迁移的情况</span><br><span class="line">伸缩：保证最小迁移数据和无法保证负载均衡（这样总共<span class="number">5</span>个节点，数据就不均匀了），翻倍扩容可以实现负载均衡</span><br></pre></td></tr></table></figure>



<h4 id="2-2-2-3-虚拟槽分区"><a href="#2-2-2-3-虚拟槽分区" class="headerlink" title="2.2.2 .3 虚拟槽分区"></a>2.2.2 .3 虚拟槽分区</h4><p>预设虚拟槽：每个槽映射一个数据子集，一般比节点数大</p>
<p>良好的哈希函数：如CRC16</p>
<p>服务端管理节点、槽、数据：如redis cluster（槽的范围0–16383）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5</span>个节点，把<span class="number">16384</span>个槽平均分配到每个节点，客户端会把数据发送给任意一个节点，通过CRC16对key进行哈希对<span class="number">16383</span>进行取余，算出当前key属于哪部分槽，属于哪个节点，每个节点都会记录是不是负责这部分槽，如果是负责的，进行保存，如果槽不在自己范围内，redis cluster是共享消息的模式，它知道哪个节点负责哪些槽，返回结果，让客户端找对应的节点去存</span><br><span class="line">服务端管理节点，槽，关系</span><br></pre></td></tr></table></figure>

<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfzpidedj31sa0ss7v2.jpg" alt="image-20200811190745066" style="zoom:30%;" />

<h2 id="三-集群搭建"><a href="#三-集群搭建" class="headerlink" title="三 集群搭建"></a>三 集群搭建</h2><h3 id="3-1-单机架构"><a href="#3-1-单机架构" class="headerlink" title="3. 1 单机架构"></a>3. 1 单机架构</h3><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfzu8mifj318m0qqwni.jpg" alt="image-20200811191353220" style="zoom:30%;" />

<h3 id="3-2-分布式架构"><a href="#3-2-分布式架构" class="headerlink" title="3.2 分布式架构"></a>3.2 分布式架构</h3><p>每个节点之间相互通信，都负责读写，客户端去存，如果不是当前节点，会返回应该存到哪个节点</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicfzygmc8j30na0qmdso.jpg" alt="image-20200811191435244" style="zoom:30%;" />

<h3 id="3-3-Redis-Cluster架构"><a href="#3-3-Redis-Cluster架构" class="headerlink" title="3.3 Redis Cluster架构"></a>3.3 Redis Cluster架构</h3><p>节点，meet，指派槽，复制，高可用</p>
<h4 id="meet解释"><a href="#meet解释" class="headerlink" title="meet解释"></a>meet解释</h4><p>A meet一下C，C回复一下，A meet一下B ，B回复一下，这样B和C也能相互感知，A，B，C之间就可以相关交互数据，所有节点共享消息</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicg03n445j31460rugwv.jpg" alt="image-20200811192029280" style="zoom:25%;" />

<h4 id="指派槽"><a href="#指派槽" class="headerlink" title="指派槽"></a>指派槽</h4><p>总共有16384个槽，平均分配到每个节点上</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicg07cegpj31i10u01kx.jpg" alt="image-20200811192334455" style="zoom:25%;" />

<h3 id="3-4-原生安装"><a href="#3-4-原生安装" class="headerlink" title="3.4 原生安装"></a>3.4 原生安装</h3><p>1 配置开启节点</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1 配置</span></span><br><span class="line">port $&#123;port&#125;</span><br><span class="line">daemonize yes</span><br><span class="line">dir <span class="string">"/opt/redis/redis/data/"</span></span><br><span class="line">logfile <span class="string">"$&#123;port&#125;.log"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#masterauth  集群搭建时，主的密码</span></span><br><span class="line">cluster-enabled yes  <span class="comment"># 开启cluster</span></span><br><span class="line">cluster-node-timeout <span class="number">15000</span> <span class="comment"># 故障转移，超时时间 15s</span></span><br><span class="line">cluster-config-file nodes-$&#123;port&#125;.conf  <span class="comment"># 给cluster节点增加一个自己的配置文件</span></span><br><span class="line">cluster-require-full-coverage yes  <span class="comment">#只要集群中有一个故障了，整个就不对外提供服务了，这个实际不合理，假设有50个节点，一个节点故障了，所有不提供服务了；，需要设置成no</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2 开启6个节点</span></span><br><span class="line">redis-server redis<span class="number">-7000.</span>conf</span><br><span class="line">redis-server redis<span class="number">-7001.</span>conf</span><br><span class="line">redis-server redis<span class="number">-7002.</span>conf</span><br><span class="line">redis-server redis<span class="number">-7003.</span>conf</span><br><span class="line">redis-server redis<span class="number">-7004.</span>conf</span><br><span class="line">redis-server redis<span class="number">-7005.</span>conf</span><br></pre></td></tr></table></figure>

<p>2 meet(相互通信)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7001</span></span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7002</span></span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7003</span></span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7004</span></span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7005</span></span><br></pre></td></tr></table></figure>

<p>3 指派槽</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster addslots &#123;<span class="number">0.</span>.<span class="number">.5461</span>&#125;</span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster addslots &#123;<span class="number">5462.</span>.<span class="number">.10922</span>&#125;</span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7000</span> cluster addslots &#123;<span class="number">10923.</span>.<span class="number">.16383</span>&#125;</span><br></pre></td></tr></table></figure>

<p>4 主从</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># cluster replicate node-id</span></span><br><span class="line"><span class="comment"># 让7003复制7000</span></span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7003</span> cluster replicate $&#123;node-id<span class="number">-7000</span>&#125;</span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7004</span> cluster replicate $&#123;node-id<span class="number">-7001</span>&#125;</span><br><span class="line">redis-cli -h <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> -p <span class="number">7005</span> cluster replicate $&#123;node-id<span class="number">-7002</span>&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 实操</span></span><br><span class="line">cd /opt/soft/redis/config</span><br><span class="line">vim redis<span class="number">-7000.</span>conf</span><br><span class="line"><span class="comment"># 写入</span></span><br><span class="line">port <span class="number">7000</span></span><br><span class="line">daemonize yes</span><br><span class="line">dir <span class="string">"/opt/soft/redis/data/"</span></span><br><span class="line">logfile <span class="string">"7000.log"</span></span><br><span class="line">dbfilename <span class="string">"dump-7000.rdb"</span></span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file nodes<span class="number">-7000.</span>conf</span><br><span class="line">cluster-require-full-coverage yes </span><br><span class="line"><span class="comment"># 快速生成其他配置</span></span><br><span class="line">sed <span class="string">'s/7000/7001/g'</span> redis<span class="number">-7000.</span>conf &gt; redis<span class="number">-7001.</span>conf</span><br><span class="line">sed <span class="string">'s/7000/7002/g'</span> redis<span class="number">-7000.</span>conf &gt; redis<span class="number">-7002.</span>conf</span><br><span class="line">sed <span class="string">'s/7000/7003/g'</span> redis<span class="number">-7000.</span>conf &gt; redis<span class="number">-7003.</span>conf</span><br><span class="line">sed <span class="string">'s/7000/7004/g'</span> redis<span class="number">-7000.</span>conf &gt; redis<span class="number">-7004.</span>conf</span><br><span class="line">sed <span class="string">'s/7000/7005/g'</span> redis<span class="number">-7000.</span>conf &gt; redis<span class="number">-7005.</span>conf</span><br><span class="line"><span class="comment"># 启动</span></span><br><span class="line">./src/redis-server ./config/redis<span class="number">-7000.</span>conf</span><br><span class="line">ps -ef |grep redis</span><br><span class="line">./src/redis-server ./config/redis<span class="number">-7001.</span>conf</span><br><span class="line">./src/redis-server ./config/redis<span class="number">-7002.</span>conf</span><br><span class="line">./src/redis-server ./config/redis<span class="number">-7003.</span>conf</span><br><span class="line">./src/redis-server ./config/redis<span class="number">-7004.</span>conf</span><br><span class="line">./src/redis-server ./config/redis<span class="number">-7005.</span>conf</span><br><span class="line"></span><br><span class="line"><span class="comment"># 连接其中一个，set数据（失败，因为没有分配槽）</span></span><br><span class="line">redis-cli -p <span class="number">7000</span></span><br><span class="line">set hello world <span class="comment">#报错</span></span><br><span class="line"><span class="comment"># config文件夹下出现了：nodes-7000.conf，查看一下可以看到节点的id</span></span><br><span class="line"><span class="comment"># 也可以：</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster nodes</span><br><span class="line"><span class="comment"># 也可以:查看更详细信息</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster info</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 节点握手（meet操作）</span></span><br><span class="line"><span class="comment"># 7000和7001 握手</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7001</span></span><br><span class="line"><span class="comment"># 查看握手情况</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster nodes <span class="comment"># 可以看到已经达成了握手</span></span><br><span class="line">redis-cli -p <span class="number">7002</span> cluster nodes <span class="comment"># 没有握手，还是孤立</span></span><br><span class="line"><span class="comment"># 继续握手</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7002</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7003</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7004</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7005</span></span><br><span class="line"><span class="comment"># 查看最后结果</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster info  <span class="comment"># 可以看到6个节点握手成功了</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#### 当前还是不可以读写，还没分配槽</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster addslots <span class="number">0</span> <span class="comment"># 给7000分配第0个槽</span></span><br><span class="line"><span class="comment"># 这样一个个设置太麻烦，咱们写个脚本执行</span></span><br><span class="line">mkdir script</span><br><span class="line">cd script</span><br><span class="line">vim addslots.sh</span><br><span class="line"><span class="comment"># 写入</span></span><br><span class="line">start=$<span class="number">1</span></span><br><span class="line">end=$<span class="number">2</span></span><br><span class="line">port=$<span class="number">3</span></span><br><span class="line"><span class="keyword">for</span> slot <span class="keyword">in</span> `seq $&#123;start&#125; $&#123;end&#125;`</span><br><span class="line">do</span><br><span class="line">	echo <span class="string">"slot:$&#123;slot&#125;"</span></span><br><span class="line">done</span><br><span class="line"><span class="comment"># 保存退出，测试</span></span><br><span class="line">sh addslots.sh <span class="number">0</span> <span class="number">4096</span> <span class="number">7000</span></span><br><span class="line"><span class="comment"># 写具体命令</span></span><br><span class="line">start=$<span class="number">1</span></span><br><span class="line">end=$<span class="number">2</span></span><br><span class="line">port=$<span class="number">3</span></span><br><span class="line"><span class="keyword">for</span> slot <span class="keyword">in</span> `seq $&#123;start&#125; $&#123;end&#125;`</span><br><span class="line">do</span><br><span class="line">	echo <span class="string">"slot:$&#123;slot&#125;"</span></span><br><span class="line">  redis-cli -p $&#123;port&#125; cluster addslots $&#123;slot&#125;</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="comment"># 写入</span></span><br><span class="line">sh addslots.sh <span class="number">0</span> <span class="number">5641</span> <span class="number">7000</span></span><br><span class="line"><span class="comment"># 查看</span></span><br><span class="line">redis-cli -p <span class="number">7000</span></span><br><span class="line">cluster info</span><br><span class="line">cluster nodes</span><br><span class="line"><span class="comment"># 继续分槽</span></span><br><span class="line">sh addslots.sh <span class="number">5641</span> <span class="number">10922</span> <span class="number">7001</span></span><br><span class="line">sh addslots.sh <span class="number">10923</span>  <span class="number">16383</span> <span class="number">7002</span></span><br><span class="line"><span class="comment"># 查看集群状态</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster info</span><br><span class="line"></span><br><span class="line"><span class="comment">##  配置主从</span></span><br><span class="line"><span class="comment"># 7003是7000的从</span></span><br><span class="line"><span class="comment"># 7004是7001的从</span></span><br><span class="line"><span class="comment"># 7005是7002的从</span></span><br><span class="line">redis-cli -p <span class="number">7003</span> cluster replicate <span class="number">7000</span>id</span><br><span class="line">redis-cli -p <span class="number">7004</span> cluster replicate <span class="number">7001</span>id</span><br><span class="line">redis-cli -p <span class="number">7005</span> cluster replicate <span class="number">7002</span>id</span><br><span class="line"></span><br><span class="line"><span class="comment">## 查看</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster info</span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster nodes</span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster slots <span class="comment"># 查看槽的信息</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 存放数据</span></span><br><span class="line">redis-cli -c -p <span class="number">7000</span></span><br><span class="line">set hello world <span class="comment"># 可以成功</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#### 生产环境建议 三台机器，主从不放在同一台机器上</span></span><br></pre></td></tr></table></figure>



<h3 id="3-5-官方工具安装（Ruby脚本）"><a href="#3-5-官方工具安装（Ruby脚本）" class="headerlink" title="3.5 官方工具安装（Ruby脚本）"></a>3.5 官方工具安装（Ruby脚本）</h3><h4 id="准备环境"><a href="#准备环境" class="headerlink" title="准备环境"></a>准备环境</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 下载编译安装ruby</span></span><br><span class="line">wget https://cache.ruby-lang.org/pub/ruby/<span class="number">2.5</span>/ruby<span class="number">-2.5</span><span class="number">.8</span>.tar.gz</span><br><span class="line">tar -zxvf ruby<span class="number">-2.5</span><span class="number">.8</span>.tar.gz</span><br><span class="line">cd ruby</span><br><span class="line">./configure -prefix=/usr/local/ruby</span><br><span class="line">make &amp;&amp; make install</span><br><span class="line">cd /usr/local/ruby</span><br><span class="line">cp bin/ruby /usr/local/bin  <span class="comment"># ruby类似于python3</span></span><br><span class="line">cp bin/gem /usr/local/bin   <span class="comment"># gem类似于pip</span></span><br><span class="line"></span><br><span class="line">ruby -v <span class="comment"># 检查版本</span></span><br><span class="line"><span class="comment"># 安装rubygem redis</span></span><br><span class="line"><span class="comment">### 更换gem源</span></span><br><span class="line">gem sources -l</span><br><span class="line"><span class="comment"># 移除https://rubygems.org源</span></span><br><span class="line">gem sources --remove https://rubygems.org/</span><br><span class="line"><span class="comment"># 增加https://gems.ruby-china.com/源</span></span><br><span class="line">gem sources -a https://gems.ruby-china.com/</span><br><span class="line"><span class="comment"># 查看</span></span><br><span class="line">gem sources -l</span><br><span class="line"><span class="comment">## 安装gem redis</span></span><br><span class="line">gem install redis -v <span class="number">3.3</span><span class="number">.3</span></span><br><span class="line"><span class="comment"># 查看</span></span><br><span class="line">gem list check redis gem</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装redis-trib.rb</span></span><br><span class="line">cd /opt/soft/redis/src</span><br><span class="line">./redis-trib.rb 弃用了，需要使用</span><br><span class="line"><span class="comment"># 1 表示给每个主节点配置一个从节点</span></span><br><span class="line">redis-cli --cluster create --cluster-replicas <span class="number">1</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7000</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7001</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7002</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7003</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7004</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7005</span></span><br><span class="line">yes</span><br></pre></td></tr></table></figure>



<h2 id="四-集群伸缩"><a href="#四-集群伸缩" class="headerlink" title="四 集群伸缩"></a>四 集群伸缩</h2><h3 id="伸缩原理"><a href="#伸缩原理" class="headerlink" title="伸缩原理"></a>伸缩原理</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 加入节点，删除节点：槽和数据在节点之间的移动</span></span><br></pre></td></tr></table></figure>

<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicg1469xqj31uc0tk7u6.jpg" alt="image-20200811235039641" style="zoom:30%;" />

<h3 id="集群扩容"><a href="#集群扩容" class="headerlink" title="集群扩容"></a>集群扩容</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 作用：为它迁移槽和数据实现扩容  作为从节点负责故障转移</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#1  准备新节点</span></span><br><span class="line">-集群模式</span><br><span class="line">-配置和其他节点统一</span><br><span class="line">-启动后是孤儿节点</span><br><span class="line">sed <span class="string">'s/7000/7006/g'</span> redis<span class="number">-7000.</span>conf &gt; redis<span class="number">-7006.</span>conf</span><br><span class="line">sed <span class="string">'s/7000/7007/g'</span> redis<span class="number">-7000.</span>conf &gt; redis<span class="number">-7007.</span>conf</span><br><span class="line">redis-server conf/redis<span class="number">-7006.</span>conf</span><br><span class="line">redis-server conf/redis<span class="number">-7007.</span>conf</span><br><span class="line"><span class="comment"># 孤立状态re</span></span><br><span class="line">redis-cli -p <span class="number">7006</span> cluster nodes</span><br><span class="line"><span class="comment">#2  加入集群</span></span><br><span class="line"><span class="comment">### 方式一</span></span><br><span class="line">在<span class="number">7000</span>上执行</span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7006</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7007</span></span><br><span class="line"><span class="comment">### 方式二</span></span><br><span class="line">redis-cli --cluster add-node <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7006</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7000</span></span><br><span class="line">redis-cli --cluster add-node <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7007</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7000</span></span><br><span class="line"><span class="comment"># 查看配置</span></span><br><span class="line">cluster nodes</span><br><span class="line"><span class="comment"># 把7007做为7006的从</span></span><br><span class="line">redis-cli -p <span class="number">7007</span> cluster replicate <span class="number">7006</span>的id</span><br><span class="line"><span class="comment">#3  迁移槽和数据</span></span><br><span class="line">  <span class="comment"># 槽迁移计划</span></span><br><span class="line">  <span class="comment"># 迁移数据</span></span><br><span class="line">  <span class="comment"># 添加从节点</span></span><br><span class="line">  </span><br><span class="line"> <span class="comment"># 我们不操作原生，直接使用redis-trip</span></span><br><span class="line">redis-cli --cluster reshard <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7000</span> </span><br><span class="line"><span class="comment">#打印当前集群状态</span></span><br><span class="line"><span class="comment"># 希望迁移多少个槽：4096</span></span><br><span class="line"><span class="comment"># 希望那个id是接收的：7006的id</span></span><br><span class="line"><span class="comment"># 传入source id ：all</span></span><br><span class="line"><span class="comment"># yes</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster nodes</span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster slots</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 其他：</span></span><br><span class="line">-如果想给<span class="number">7000</span>再加一个从节点怎么弄？</span><br><span class="line"><span class="comment"># 启动起7006，meet一下，让7006复制7000</span></span><br><span class="line">redis-cli -p <span class="number">7000</span> cluster meet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">7006</span></span><br><span class="line">redis-cli -p <span class="number">7006</span> cluster replicate e03fb9a259cd314e9a23e17573d07c477d3242f7</span><br></pre></td></tr></table></figure>



<h3 id="集群缩容"><a href="#集群缩容" class="headerlink" title="集群缩容"></a>集群缩容</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 下线迁槽（把7006的1366个槽迁移到7000上）</span></span><br><span class="line">redis-cli --cluster reshard --cluster-<span class="keyword">from</span> <span class="number">7006</span>的id --cluster-to <span class="number">7000</span>的id --cluster-slots <span class="number">1366</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7000</span></span><br><span class="line">yes</span><br><span class="line"></span><br><span class="line">redis-cli --cluster reshard --cluster-<span class="keyword">from</span> <span class="number">7006</span>的id --cluster-to <span class="number">7001</span>的id --cluster-slots <span class="number">1366</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7001</span></span><br><span class="line">yes</span><br><span class="line">redis-cli --cluster reshard --cluster-<span class="keyword">from</span> <span class="number">7006</span>的id --cluster-to <span class="number">7002</span>的id --cluster-slots <span class="number">1365</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7002</span></span><br><span class="line">yes</span><br><span class="line"><span class="comment"># 忘记节点，关闭节点</span></span><br><span class="line">redis-cli --cluster <span class="keyword">del</span>-node <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7000</span> 要下线的<span class="number">7007</span>id  <span class="comment"># 先下从，再下主，因为先下主会触发故障转移</span></span><br><span class="line">redis-cli --cluster <span class="keyword">del</span>-node <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">7000</span> 要下线的<span class="number">7006</span>id </span><br><span class="line"><span class="comment"># </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 关掉其中一个主，另一个从立马变成主顶上， 重启停止的主，发现变成了从</span></span><br></pre></td></tr></table></figure>



<h2 id="五-客户端连接"><a href="#五-客户端连接" class="headerlink" title="五 客户端连接"></a>五 客户端连接</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -c -p <span class="number">7000</span>  <span class="comment"># -c表示集群模式</span></span><br><span class="line">set hello world  <span class="comment"># ok</span></span><br><span class="line">cluster keyslot php</span><br><span class="line"><span class="comment"># 9244</span></span><br><span class="line">set php sb <span class="comment"># 不命中，会返回7001，自动跳转到7001上  不加-c，只会返回错误，不会去执行7001上保存</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># rediscluster</span></span><br><span class="line"><span class="comment"># pip3 install redis-py-cluster</span></span><br><span class="line"><span class="keyword">from</span> rediscluster <span class="keyword">import</span> RedisCluster</span><br><span class="line">startup_nodes = [&#123;<span class="string">"host"</span>:<span class="string">"127.0.0.1"</span>, <span class="string">"port"</span>: <span class="string">"7000"</span>&#125;,&#123;<span class="string">"host"</span>:<span class="string">"127.0.0.1"</span>, <span class="string">"port"</span>: <span class="string">"7001"</span>&#125;,&#123;<span class="string">"host"</span>:<span class="string">"127.0.0.1"</span>, <span class="string">"port"</span>: <span class="string">"7002"</span>&#125;]</span><br><span class="line"><span class="comment"># rc = RedisCluster(startup_nodes=startup_nodes,decode_responses=True)</span></span><br><span class="line">rc = RedisCluster(startup_nodes=startup_nodes)</span><br><span class="line">rc.set(<span class="string">"foo"</span>, <span class="string">"bar"</span>)</span><br><span class="line">print(rc.get(<span class="string">"foo"</span>))</span><br></pre></td></tr></table></figure>



<h2 id="六-集群原理"><a href="#六-集群原理" class="headerlink" title="六 集群原理"></a>六 集群原理</h2><p>move重定向</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicg19v43hj31pl0u01ea.jpg" alt="image-20200812003219180" style="zoom:30%;" />



<p>槽命中</p>
<p>cluster keyslot hello 可以计算出槽的值</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicg1dtp92j31te0q27hv.jpg" alt="image-20200812003318203" style="zoom:30%;" />



<p>槽不命中：moved异常</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gicg1i8mu1j31r80u07nz.jpg" alt="image-20200812003446448" style="zoom:30%;" />

<h2 id="七-补充"><a href="#七-补充" class="headerlink" title="七 补充"></a>七 补充</h2><h3 id="7-1-5-0以后集群搭建"><a href="#7-1-5-0以后集群搭建" class="headerlink" title="7.1 5.0以后集群搭建"></a>7.1 5.0以后集群搭建</h3><p>Redis Cluster 在5.0之后取消了ruby脚本 <strong>redis-trib.rb</strong>的支持（手动命令行添加集群的方式不变），集合到redis-cli里，避免了再安装ruby的相关环境。直接使用redis-clit的参数–cluster 来取代</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster help</span><br><span class="line">Cluster Manager Commands:</span><br><span class="line">  create         host1:port1 ... hostN:portN   <span class="comment">#创建集群</span></span><br><span class="line">                 --cluster-replicas &lt;arg&gt;      <span class="comment">#从节点个数</span></span><br><span class="line">  check          host:port                     <span class="comment">#检查集群</span></span><br><span class="line">                 --cluster-search-multiple-owners <span class="comment">#检查是否有槽同时被分配给了多个节点</span></span><br><span class="line">  info           host:port                     <span class="comment">#查看集群状态</span></span><br><span class="line">  fix            host:port                     <span class="comment">#修复集群</span></span><br><span class="line">                 --cluster-search-multiple-owners <span class="comment">#修复槽的重复分配问题</span></span><br><span class="line">  reshard        host:port                     <span class="comment">#指定集群的任意一节点进行迁移slot，重新分slots</span></span><br><span class="line">                 --cluster-<span class="keyword">from</span> &lt;arg&gt;          <span class="comment">#需要从哪些源节点上迁移slot，可从多个源节点完成迁移，以逗号隔开，传递的是节点的node id，还可以直接传递--from all，这样源节点就是集群的所有节点，不传递该参数的话，则会在迁移过程中提示用户输入</span></span><br><span class="line">                 --cluster-to &lt;arg&gt;            <span class="comment">#slot需要迁移的目的节点的node id，目的节点只能填写一个，不传递该参数的话，则会在迁移过程中提示用户输入</span></span><br><span class="line">                 --cluster-slots &lt;arg&gt;         <span class="comment">#需要迁移的slot数量，不传递该参数的话，则会在迁移过程中提示用户输入。</span></span><br><span class="line">                 --cluster-yes                 <span class="comment">#指定迁移时的确认输入</span></span><br><span class="line">                 --cluster-timeout &lt;arg&gt;       <span class="comment">#设置migrate命令的超时时间</span></span><br><span class="line">                 --cluster-pipeline &lt;arg&gt;      <span class="comment">#定义cluster getkeysinslot命令一次取出的key数量，不传的话使用默认值为10</span></span><br><span class="line">                 --cluster-replace             <span class="comment">#是否直接replace到目标节点</span></span><br><span class="line">  rebalance      host:port                                      <span class="comment">#指定集群的任意一节点进行平衡集群节点slot数量 </span></span><br><span class="line">                 --cluster-weight &lt;node1=w1...nodeN=wN&gt;         <span class="comment">#指定集群节点的权重</span></span><br><span class="line">                 --cluster-use-empty-masters                    <span class="comment">#设置可以让没有分配slot的主节点参与，默认不允许</span></span><br><span class="line">                 --cluster-timeout &lt;arg&gt;                        <span class="comment">#设置migrate命令的超时时间</span></span><br><span class="line">                 --cluster-simulate                             <span class="comment">#模拟rebalance操作，不会真正执行迁移操作</span></span><br><span class="line">                 --cluster-pipeline &lt;arg&gt;                       <span class="comment">#定义cluster getkeysinslot命令一次取出的key数量，默认值为10</span></span><br><span class="line">                 --cluster-threshold &lt;arg&gt;                      <span class="comment">#迁移的slot阈值超过threshold，执行rebalance操作</span></span><br><span class="line">                 --cluster-replace                              <span class="comment">#是否直接replace到目标节点</span></span><br><span class="line">  add-node       new_host:new_port existing_host:existing_port  <span class="comment">#添加节点，把新节点加入到指定的集群，默认添加主节点</span></span><br><span class="line">                 --cluster-slave                                <span class="comment">#新节点作为从节点，默认随机一个主节点</span></span><br><span class="line">                 --cluster-master-id &lt;arg&gt;                      <span class="comment">#给新节点指定主节点</span></span><br><span class="line">  <span class="keyword">del</span>-node       host:port node_id                              <span class="comment">#删除给定的一个节点，成功后关闭该节点服务</span></span><br><span class="line">  call           host:port command arg arg .. arg               <span class="comment">#在集群的所有节点执行相关命令</span></span><br><span class="line">  set-timeout    host:port milliseconds                         <span class="comment">#设置cluster-node-timeout</span></span><br><span class="line">  <span class="keyword">import</span>         host:port                                      <span class="comment">#将外部redis数据导入集群</span></span><br><span class="line">                 --cluster-<span class="keyword">from</span> &lt;arg&gt;                           <span class="comment">#将指定实例的数据导入到集群</span></span><br><span class="line">                 --cluster-copy                                 <span class="comment">#migrate时指定copy</span></span><br><span class="line">                 --cluster-replace                              <span class="comment">#migrate时指定replace</span></span><br><span class="line">  help           </span><br><span class="line"></span><br><span class="line">For check, fix, reshard, <span class="keyword">del</span>-node, set-timeout you can specify the host <span class="keyword">and</span> port of any working node <span class="keyword">in</span> the cluster.</span><br></pre></td></tr></table></figure>

<p>① 创建集群主节点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster create 192.168.163.132:6379 192.168.163.132:6380 192.168.163.132:6381</span><br></pre></td></tr></table></figure>

<p>② 创建集群主从节点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;redis-cli --cluster create 192.168.163.132:6379 192.168.163.132:6380 192.168.163.132:6381 192.168.163.132:6382 192.168.163.132:6383 192.168.163.132:6384 --cluster-replicas 1</span><br></pre></td></tr></table></figure>

<p>说明：–cluster-replicas 参数为数字，1表示每个主节点需要1个从节点。</p>
<p>通过该方式创建的带有从节点的机器不能够自己手动指定主节点，所以如果需要指定的话，需要自己手动指定，先使用①或③创建好主节点后，再通过④来处理。</p>
<p>③ 添加集群主节点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster add-node 192.168.163.132:6382 192.168.163.132:6379</span><br></pre></td></tr></table></figure>

<p>说明：为一个指定集群添加节点，需要先连到该集群的任意一个节点IP（192.168.163.132:6379），再把新节点加入。该2个参数的顺序有要求：新加入的节点放前</p>
<p>④ 添加集群从节点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster add-node 192.168.163.132:6382 192.168.163.132:6379 --cluster-slave --cluster-master-id 117457eab5071954faab5e81c3170600d5192270</span><br></pre></td></tr></table></figure>

<p>说明：把6382节点加入到6379节点的集群中，并且当做node_id为 117457eab5071954faab5e81c3170600d5192270 的从节点。如果不指定 <strong>–cluster-master-id</strong> 会随机分配到任意一个主节点。</p>
<p>⑤ 删除节点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster del-node 192.168.163.132:6384 f6a6957421b80409106cb36be3c7ba41f3b603ff</span><br></pre></td></tr></table></figure>

<p>说明：指定IP、端口和node_id 来删除一个节点，从节点可以直接删除，主节点不能直接删除，删除之后，该节点会被shutdown。</p>
<p>注意：当被删除掉的节点重新起来之后不能自动加入集群，但其和主的复制还是正常的，也可以通过该节点看到集群信息（通过其他正常节点已经看不到该被del-node节点的信息）。</p>
<p>如果想要再次加入集群，则需要先在该节点执行cluster reset，再用add-node进行添加，进行增量同步复制。</p>
<p>到此，目前整个集群的状态如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">192.168.163.132:6379&gt; cluster nodes</span><br><span class="line">815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380@16380 master - 0 1569748297177 2 connected 5461-10922</span><br><span class="line">0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383@16383 slave 56005b9413cbf225783906307a2631109e753f8f 0 1569748295000 4 connected</span><br><span class="line">3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382@16382 slave 815da8448f5d5a304df0353ca10d8f9b77016b28 0 1569748295000 5 connected</span><br><span class="line">117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379@16379 myself,master - 0 1569748297000 1 connected 0-5460</span><br><span class="line">56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381@16381 master - 0 1569748295000 3 connected 10923-16383</span><br><span class="line">f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384@16384 slave 117457eab5071954faab5e81c3170600d5192270 0 1569748298185 6 connected</span><br></pre></td></tr></table></figure>



<p>⑥ 检查集群</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster check 192.168.163.132:6384 --cluster-search-multiple-owners</span><br></pre></td></tr></table></figure>

<p>说明：任意连接一个集群节点，进行集群状态检查</p>
<p>⑦ 集群信息查看</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster info 192.168.163.132:6384</span><br></pre></td></tr></table></figure>

<p>说明：检查key、slots、从节点个数的分配情况</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;redis-cli --cluster info 192.168.163.132:6384</span><br><span class="line">192.168.163.132:6380 (815da844...) -&gt; 0 keys | 5462 slots | 1 slaves.</span><br><span class="line">192.168.163.132:6381 (56005b94...) -&gt; 0 keys | 5461 slots | 1 slaves.</span><br><span class="line">192.168.163.132:6379 (117457ea...) -&gt; 2 keys | 5461 slots | 1 slaves.</span><br><span class="line">[OK] 2 keys in 3 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br></pre></td></tr></table></figure>

<p>⑧ 修复集群</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster fix 192.168.163.132:6384 --cluster-search-multiple-owners</span><br></pre></td></tr></table></figure>

<p>说明：修复集群和槽的重复分配问题</p>
<p>⑨ 设置集群的超时时间 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster set-timeout 192.168.163.132:6382 10000</span><br></pre></td></tr></table></figure>

<p>说明：连接到集群的任意一节点来设置集群的超时时间参数cluster-node-timeout</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster set-timeout 192.168.163.132:6382 10000</span><br><span class="line">&gt;&gt;&gt; Reconfiguring node timeout in every cluster node...</span><br><span class="line">*** New timeout set for 192.168.163.132:6382</span><br><span class="line">*** New timeout set for 192.168.163.132:6384</span><br><span class="line">*** New timeout set for 192.168.163.132:6383</span><br><span class="line">*** New timeout set for 192.168.163.132:6379</span><br><span class="line">*** New timeout set for 192.168.163.132:6381</span><br><span class="line">*** New timeout set for 192.168.163.132:6380</span><br><span class="line">&gt;&gt;&gt; New node timeout set. 6 OK, 0 ERR.</span><br></pre></td></tr></table></figure>



<p>⑩ 集群中执行相关命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster call 192.168.163.132:6381 config set requirepass cc</span><br><span class="line">redis-cli -a cc --cluster call 192.168.163.132:6381 config set masterauth cc</span><br><span class="line">redis-cli -a cc --cluster call 192.168.163.132:6381 config rewrite</span><br></pre></td></tr></table></figure>

<p>说明：连接到集群的任意一节点来对整个集群的所有节点进行设置。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster call 192.168.163.132:6381 config set cluster-node-timeout 12000</span><br><span class="line">&gt;&gt;&gt; Calling config set cluster-node-timeout 12000</span><br><span class="line">192.168.163.132:6381: OK</span><br><span class="line">192.168.163.132:6383: OK</span><br><span class="line">192.168.163.132:6379: OK</span><br><span class="line">192.168.163.132:6384: OK</span><br><span class="line">192.168.163.132:6382: OK</span><br><span class="line">192.168.163.132:6380: OK</span><br><span class="line">...</span><br><span class="line">...</span><br></pre></td></tr></table></figure>



<p>到此，相关集群的基本操作已经介绍完，现在说明集群迁移的相关操作。</p>
<h3 id="迁移相关"><a href="#迁移相关" class="headerlink" title="迁移相关"></a>迁移相关</h3><p>① <strong>在线迁移slot</strong> ：在线把集群的一些slot从集群原来slot节点迁移到新的节点，即可以完成集群的在线横向扩容和缩容。有2种方式进行迁移</p>
<p>一是根据提示来进行操作：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">直接连接到集群的任意一节点</span><br><span class="line">redis-cli -a cc --cluster reshard 192.168.163.132:6379</span><br></pre></td></tr></table></figure>

<p>信息如下：</p>
<p>二是根据参数进行操作：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -a cc --cluster reshard 192.168.163.132:6379 --cluster-from 117457eab5071954faab5e81c3170600d5192270 --cluster-to 815da8448f5d5a304df0353ca10d8f9b77016b28 --cluster-slots 10 --cluster-yes --cluster-timeout 5000 --cluster-pipeline 10 --cluster-replace</span><br></pre></td></tr></table></figure>

<p>说明：连接到集群的任意一节点来对指定节点指定数量的slot进行迁移到指定的节点。 </p>
<p>② 平衡（rebalance）<strong>slot</strong> ：</p>
<p>1）平衡集群中各个节点的slot数量</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -a cc --cluster rebalance 192.168.163.132:6379</span><br></pre></td></tr></table></figure>



<p> 2）根据集群中各个节点设置的权重等平衡slot数量（不执行，只模拟）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -a cc --cluster rebalance --cluster-weight 117457eab5071954faab5e81c3170600d5192270&#x3D;5 815da8448f5d5a304df0353ca10d8f9b77016b28&#x3D;4 56005b9413cbf225783906307a2631109e753f8f&#x3D;3 --cluster-simulate 192.168.163.132:6379</span><br></pre></td></tr></table></figure>

<p>③ 导入集群</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster import 192.168.163.132:6379 --cluster-from 192.168.163.132:9021 --cluster-replace</span><br></pre></td></tr></table></figure>

<p>说明：外部Redis实例（9021）导入到集群中的任意一节点。</p>
<p>注意：测试下来发现参数–cluster-replace没有用，如果集群中已经包含了某个key，在导入的时候会失败，不会覆盖，只有清空集群key才能导入。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">*** Importing 97847 keys from DB 0</span><br><span class="line">Migrating 9223372011174675807 to 192.168.163.132:6381: Source 192.168.163.132:9021 replied with error:</span><br><span class="line">ERR Target instance replied with error: BUSYKEY Target key name already exists</span><br></pre></td></tr></table></figure>

<p>并且发现如果集群设置了密码，也会导入失败，需要设置集群密码为空才能进行导入（call）。通过monitor（9021）的时候发现，在migrate的时候需要密码进行auth认证。 </p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/db/Redis%E7%B3%BB%E5%88%97/06-Redis%E7%B3%BB%E5%88%97%E4%B9%8B%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E5%8E%9F%E7%90%86%E4%B8%8E%E4%BC%98%E5%8C%96/">
        <span class="nav-arrow">← </span>
        
          db/Redis系列/06-Redis系列之主从复制原理与优化
        
      </a>
    
    
      <a class="nav-right" href="/db/Redis%E7%B3%BB%E5%88%97/07-Redis%E7%B3%BB%E5%88%97%E4%B9%8B-Redis-Sentinel/">
        
          db/Redis系列/07-Redis系列之-Redis-Sentinel
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#一-Redis-Cluser介绍背景"><span class="toc-nav-text">一  Redis Cluser介绍背景</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-1问题"><span class="toc-nav-text">1.1问题</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-2-解决"><span class="toc-nav-text">1.2 解决</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#二-数据分布（分布式数据库）"><span class="toc-nav-text">二 数据分布（分布式数据库）</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-1-存在问题"><span class="toc-nav-text">2.1 存在问题</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-2-分区方式"><span class="toc-nav-text">2.2 分区方式</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#2-2-1-顺序分区"><span class="toc-nav-text">2.2.1 顺序分区</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-2-2-哈希分区"><span class="toc-nav-text">2.2.2 哈希分区</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#2-2-2-1-节点取余分区"><span class="toc-nav-text">2.2.2 .1 节点取余分区</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#2-2-2-2-一致性哈希分区"><span class="toc-nav-text">2.2.2 .2 一致性哈希分区</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#2-2-2-3-虚拟槽分区"><span class="toc-nav-text">2.2.2 .3 虚拟槽分区</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#三-集群搭建"><span class="toc-nav-text">三 集群搭建</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-1-单机架构"><span class="toc-nav-text">3. 1 单机架构</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-2-分布式架构"><span class="toc-nav-text">3.2 分布式架构</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-3-Redis-Cluster架构"><span class="toc-nav-text">3.3 Redis Cluster架构</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#meet解释"><span class="toc-nav-text">meet解释</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#指派槽"><span class="toc-nav-text">指派槽</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-4-原生安装"><span class="toc-nav-text">3.4 原生安装</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-5-官方工具安装（Ruby脚本）"><span class="toc-nav-text">3.5 官方工具安装（Ruby脚本）</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#准备环境"><span class="toc-nav-text">准备环境</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#四-集群伸缩"><span class="toc-nav-text">四 集群伸缩</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#伸缩原理"><span class="toc-nav-text">伸缩原理</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#集群扩容"><span class="toc-nav-text">集群扩容</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#集群缩容"><span class="toc-nav-text">集群缩容</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#五-客户端连接"><span class="toc-nav-text">五 客户端连接</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#六-集群原理"><span class="toc-nav-text">六 集群原理</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#七-补充"><span class="toc-nav-text">七 补充</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#7-1-5-0以后集群搭建"><span class="toc-nav-text">7.1 5.0以后集群搭建</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#迁移相关"><span class="toc-nav-text">迁移相关</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/db/Redis系列/08-Redis系列之-Redis-Cluster/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "db/Redis系列/08-Redis系列之-Redis-Cluster",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>